/**
 * 显示玩家信息
 */
class playerPanel extends eui.Component {
	public constructor(superDisplay) {
		super();
		this.skinName = "resource/assets/skins/playerCtrlSkin.exml";
		this.width = GlobalData.GameStage_width;
		this.height = GlobalData.GameStage_height;
		st_config.setScreenAuto(this);
		this.init();
		this.once(egret.Event.ADDED_TO_STAGE, this.function_keys_init, this);
		this.superLayer = superDisplay;
	}

	private superLayer;
	/**房间计时 */
	public gameCountDownTime: eui.Label;
	/**玩家舌头长度 */
	private userTongueLength: eui.Label;
	/**帧数显示 */
	private zhenshu: eui.Label;
	/**信号显示 */
	private xinHao: eui.Image;
	/**队友指示箭头 */
	public teammateMark: eui.Group;
	/**青蛙标记 */
	public frogTag: eui.Image;
	/**房间号 */
	private roomNumber: eui.Label;
	//---------------------------------------功能键
	/**吐舌按钮 */
	public ligature: eui.Button;
	/**吐舌光圈计时器 */
	public shootHaloTimer: number;
	/**吐舌光圈开关 */
	public shootHaloSwitch: boolean;
	/**断线按钮 */
	public breakLine: eui.Button;
	/**退出房间 */
	public backToHall: eui.Image;
	/**新手引导跳过按钮 */
	private skip: eui.Button;
	/**冷却值 */
	public coolDown: eui.Image;
	/**冷却值遮罩 */
	public coolDownMask: egret.Shape;
	/**----------------------------------单人排行榜 */
	public top_Users: eui.Group;
	/**其他玩家排行名字 */
	private otherUserName: eui.Label;
	/**其他玩家排行分数 */
	private otherUserGrade: eui.Label;
	/**我自己的排行名字和排名 */
	private myMsg: eui.Label;
	/**我自己的排行分数 */
	private myGrade: eui.Label;



	/**倒计时 */
	public countDownTime = 360;
	/**吐舌遮罩角度 */
	public shootAngle: number = 0;

	public checkServerHertBeat: egret.Timer = new egret.Timer(1000, 0);

	/**错误内容 */
	private err_errtext: any;
	private beijing: eui.Image;
	private killMan: eui.Image;
	private killNum: eui.Label;
	/**背景音乐 */
	private bgSound: soundMgr = new soundMgr();
	private nowTime;
	public c = serverConfig.config;
	private disCd: eui.Label;
	private sm: any;
	private init() {
		this.resetUserTop();
		this.teammateMark.visible = false;
		this.frogTag.visible = false;
		this.ligature.visible = false;
		this.breakLine.visible = false;
		this.roomNumber.visible = false;
		if (webNetworkMananger.isServerMove) {
			this.xinHao.visible = true;
			this.showRoomId();
			this.roomNumber.visible = true;
		}
		SoundManager.getInstance().playBgSound(false, "1");
		SoundManager.getInstance().playBgSound(true, "main_mp3");
		SoundManager.getInstance().scene = 1;
		this.killMan.visible = false;
		this.killNum.visible = false;
		this.disCd.visible = false;
		this.addChild(this.disCd);
		//如果是团队模式打开5v5排行榜
		this.top_Users.visible = true;
		//this.function_keys_init();
		this.backTohallInit();
		if (DataProl.isXinShou) {
			this.skip.visible = true;
		}
		this.shootHaloSwitch = false;
	}
	/**更新 */
	public update() {
		//新手引导 吐舌光圈
		if (DataProl.isXinShou) {
			if (this.shootHaloSwitch) {
				//第一次
				if (this.shootHaloTimer == null) {
					this.shootHaloTimer = Date.now();
					egret.Tween.get(this.ligature).to({ alpha: 1 }, 1000, egret.Ease.sineIn).to({ alpha: 0.3, }, 1000, egret.Ease.sineIn);
					egret.Tween.get(this.breakLine).to({ alpha: 1 }, 1000, egret.Ease.sineIn).to({ alpha: 0.3, }, 1000, egret.Ease.sineIn);
				} else {
					if (Date.now() - this.shootHaloTimer > 2000) {
						this.shootHaloTimer = Date.now();
						egret.Tween.get(this.ligature).to({ alpha: 1 }, 1000, egret.Ease.sineIn).to({ alpha: 0.3 }, 1000, egret.Ease.sineIn);
						egret.Tween.get(this.breakLine).to({ alpha: 1 }, 1000, egret.Ease.sineIn).to({ alpha: 0.3, }, 1000, egret.Ease.sineIn);
					}
				}
			} else {
				this.shootHaloTimer = null;
				egret.Tween.removeTweens(this.ligature);
				egret.Tween.removeTweens(this.breakLine);
				this.lock_tongue();
			}

		}
	}
	/**功能键初始化 */
	private function_keys_init() {
		this.shootAngle = 0;
		this.coolDownMask = new egret.Shape();
		this.addChild(this.coolDownMask);
		this.coolDown.mask = this.coolDownMask;
		this.coolDown.visible = false;
		this.ligature.addEventListener(egret.TouchEvent.TOUCH_TAP, this.tongue, this);
		this.breakLine.addEventListener(egret.TouchEvent.TOUCH_TAP, this.offLine, this);
		this.skip.addEventListener(egret.TouchEvent.TOUCH_TAP, this.newbieSkip, this);
	}


	/**跳过 */
	private newbieSkip() {
		if (DataProl.isXinShou) {
			if (this.superLayer.prop_messageTimes < 8) {
				this.superLayer.set_scene2();
			}
			else {
				this.superLayer.displayer_message(15, 500, 500);
			}

		}
	}
	/**初始化模式选择 */
	public initializeModeSelection() {
		DataProl.isXinShou = false;
		DataProl.isSingleGame = false;
		DataProl.isDoubleRocker = false;
		DataProl.tuanduizhan = false;
		webNetworkMananger.isServerMove = false;
		DataProl.isCreateRockerPlayer = false;
		DataProl.isOpenRockerjudge = false;
		DataProl.loginType = 0;
		DataProl.friendId = -1;
	}
	/**吐舌 */
	private tongue() {
		if (DataProl.isXinShou) {
			this.lock_tongue();
		}
		if (webNetworkMananger.isServerMove) {
			this.superLayer.playerLoll();
			//------------------------------------冷却值
			if (DataProl.isXinShou) {//新手引导取消吐舌冷却
				return;
			}
			egret.Tween.get(this, { onChange: this.show_cd, onChangeObj: this }).to({ shootAngle: 360 }, 3000).call(this.restLine);
			this.ligature.touchEnabled = false;
			egret.Tween.get(this.ligature).to({ scaleX: 1.1, scaleY: 1.1 }, 50).to({ scaleX: 1, scaleY: 1, alpha: 0.5 }, 50);
			//this.ligature.alpha = 0.5;
			this.coolDownMask.x = this.coolDown.x;
			this.coolDownMask.y = this.coolDown.y;
			this.coolDown.visible = true;
		}
		else {
			if (AIServer.getInstance().getUserStates(DataProl.account) == 0) {
				this.superLayer.playerLoll();
				//------------------------------------冷却值
				if (DataProl.isXinShou) {//新手引导取消吐舌冷却
					return;
				}
				egret.Tween.get(this, { onChange: this.show_cd, onChangeObj: this }).to({ shootAngle: 360 }, 3000).call(this.restLine);
				this.ligature.touchEnabled = false;
				egret.Tween.get(this.ligature).to({ scaleX: 1.1, scaleY: 1.1 }, 50).to({ scaleX: 1, scaleY: 1, alpha: 0.5 }, 50);
				//this.ligature.alpha = 0.5;
				this.coolDownMask.x = this.coolDown.x;
				this.coolDownMask.y = this.coolDown.y;
				this.coolDown.visible = true;
			}
		}

	}
	/**CD显示 */
	public show_cd() {
		let shape = this.coolDownMask;

		//	console.log("btn" + this.coolDown.x);
		let angle = this.shootAngle;
		shape.graphics.clear();

		shape.graphics.beginFill(0x00ffff, 1);
		shape.graphics.moveTo(0, 0);
		shape.graphics.lineTo(this.coolDown.width / 2, 0);
		shape.graphics.drawArc(0, 0, this.coolDown.width / 2, 0, angle * Math.PI / 180, true);
		shape.graphics.lineTo(0, 0);
		shape.graphics.endFill();

	}
	private createBitmapByName(name: string): egret.Bitmap {
		let result = new egret.Bitmap();
		let texture: egret.Texture = RES.getRes(name);
		result.texture = texture;
		return result;
	}
	/**隐藏新手引导的部分引导*/
	public hideGuideUi() {
		this.beijing.visible = false;
		this.userTongueLength.visible = false;
		this.top_Users.visible = false;//不显示排行榜
		this.backToHall.visible = false;
	}
	/**重置吐舌 */
	public restLine() {

		egret.Tween.get(this.ligature).to({ scaleX: 1.1, scaleY: 1.1 }, 50).to({ scaleX: 1, scaleY: 1, alpha: 1 }, 50);
		this.shootAngle = 0;
		this.coolDown.visible = false;
		this.ligature.touchEnabled = true;
	}
	/**吐舌按钮锁定 */
	public lock_tongue() {
		this.shootHaloSwitch = false;
		this.ligature.touchEnabled = false;
		this.ligature.alpha = 0;
		this.ligature.visible = false;
		this.breakLine.touchEnabled = false;
		this.breakLine.alpha = 0;
		this.breakLine.visible = false;
		this.coolDownMask.visible = false;
	}
	/**吐舌按钮解锁 */
	public unlock_tongue() {
		this.shootHaloSwitch = true;
		this.ligature.touchEnabled = true;
		//	this.ligature.visible = true;
		this.ligature.alpha = 0.3;
		this.breakLine.touchEnabled = true;
		this.breakLine.alpha = 0.3;
		//	this.breakLine.visible = true;
		this.coolDownMask.visible = true;
	}
	/**跳过按钮解锁 */
	public unlock_skip() {
		this.skip.touchEnabled = true;
		this.skip.alpha = 1;
	}
	/**跳过按钮锁定 */
	public lock_skip() {
		this.skip.touchEnabled = false;
		this.skip.alpha = 0;
	}
	/**断线 */
	private offLine() {
		if (DataProl.isXinShou) {
			this.lock_tongue();
			this.superLayer.displayer_message(13, 1100, 600);
		}
		egret.Tween.removeTweens(this);
		if (webNetworkMananger.isServerMove) {
			let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
			let m_c2sDisconnect = new awesomepackage.c2sDisconnect();
			m_c2sDisconnect.account = DataProl.account;
			m_c2sDisconnect.roomId = DataProl.roomid;
			m_AwesomeMessage.meesageId = 1031;
			m_AwesomeMessage.data = awesomepackage.c2sDisconnect.encode(m_c2sDisconnect).finish();
			var buffer = awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
			var byte2: egret.ByteArray = new egret.ByteArray(buffer);
			webNetworkMananger.getInstance().SendBitMeesage(byte2);
		} else {
			AIServer.getInstance().breakLine(DataProl.account);
			if (DataProl.isXinShou) {
				return;
			}
			this.ligature.touchEnabled = false;
			this.ligature.alpha = 0.5;
			this.shootAngle = 0;
			egret.Tween.get(this, { onChange: this.show_cd, onChangeObj: this }).to({ shootAngle: 360 }, 3000).call(this.restLine);
			this.coolDown.visible = true;
		}

	}
	/**显示房间号 */
	public showRoomId() {
		if (DataProl.roomid != null && DataProl.roomid != undefined) {
			this.roomNumber.text = "房间号:" + DataProl.roomid;
		} else {
			this.roomNumber.text = "房间号:未知";
		}
	}

	private teammateMarkUpdatePoint = new egret.Point();
	/**队友指示箭头更新
	 * @param wallNum 1-上墙 2-右墙 3-下墙 4-左墙
	 */
	public teammateMarkUpdate(teammateByGlobal: egret.Point, rotation: number, wallNum: number) {
		/**基于面板的本地坐标 */
		let teammateMarkBylocal = this.globalToLocal(teammateByGlobal.x, teammateByGlobal.y);
		this.teammateMark.x = teammateMarkBylocal.x;
		this.teammateMark.y = teammateMarkBylocal.y;
		//this.teammateMark.rotation = rotation;
		let frogTagPoint = this.teammateMark.localToGlobal(17.5, 22, this.teammateMarkUpdatePoint);
		let frogTagPoint_local = this.frogTag.parent.globalToLocal(frogTagPoint.x, frogTagPoint.y);
		this.frogTag.x = frogTagPoint_local.x;
		this.frogTag.y = frogTagPoint_local.y;
		switch (wallNum) {
			case 1:
				if (teammateByGlobal.x > this.stage.stageWidth / 2) {
					this.teammateMark.scaleX = 1;
					this.teammateMark.scaleY = 1;
				} else {
					this.teammateMark.scaleX = -1;
					this.teammateMark.scaleY = 1;
				}
				break;
			case 2:
				if (teammateByGlobal.y > this.stage.stageHeight / 2) {
					this.teammateMark.scaleX = -1;
					this.teammateMark.scaleY = 1;
				} else {
					this.teammateMark.scaleX = -1;
					this.teammateMark.scaleY = -1;
				}
				break;
			case 3:
				if (teammateByGlobal.x > this.stage.stageWidth / 2) {
					this.teammateMark.scaleX = 1;
					this.teammateMark.scaleY = -1;
				} else {
					this.teammateMark.scaleX = -1;
					this.teammateMark.scaleY = -1;
				}
				break;
			case 4:
				if (teammateByGlobal.y > this.stage.stageHeight / 2) {
					this.teammateMark.scaleX = 1;
					this.teammateMark.scaleY = 1;
				} else {
					this.teammateMark.scaleX = 1;
					this.teammateMark.scaleY = -1;
				}
				break;
		}
		this.teammateMark.visible = true;
		this.frogTag.visible = true;
	}


	/**重置单人排行榜 */
	public resetUserTop() {
		this.otherUserName.text = null;
		this.otherUserGrade.text = null;
		this.myMsg.text = null;
		this.myMsg.textColor = 0x00FF00;
		this.myGrade.text = null;
		this.myGrade.textColor = 0x00FF00;
	}

	// }
	/**信号——帧数显示 */
	public show_frame_number() {
		if (DataProl.frameNumber != null) {
			this.zhenshu.text = "逻辑帧数:" + DataProl.frameNumber;
			if (DataProl.frameNumber >= 7) {
				this.xinHao.source = "xinhao_3_png";
			} else if (DataProl.frameNumber >= 4) {
				this.xinHao.source = "xinhao_2_png";
			} else {
				this.xinHao.source = "xinhao_1_png";
			}

		} else {
			this.xinHao.source = "xinhao_1_png";
		}
	}
	/**是否要变小  测试用 */
	private isSmall: boolean = true;

	/**显示CD */
	private displayCD(tim) {
		this.disCd.text = tim.toString();
		if (tim >= 0)
			setTimeout(() => { this.displayCD(tim - 1); }, 1000);
		if (tim < 0)
			this.disCd.visible = false;;
	}
	private roomTimer: egret.Timer = new egret.Timer(1000, -1);

	/**开启房间计时器 */
	public startRoomTimer() {
		this.roomTimer.reset();
		this.roomTimer.removeEventListener(egret.TimerEvent.TIMER, this.roomTimeLoop, this);
		this.countDownTime = DataProl.roomStartTime;
		var nowTime = (new Date()).valueOf();
		var timeLag = nowTime - this.countDownTime;
		if (webNetworkMananger.isServerMove) {
			timeLag = 0;
		}
		this.countDownTime = (DataProl.roomTime / 1000) - Math.floor(timeLag / 1000);
		this.displayCountDownInfo(this.countDownTime--);
		this.roomTimer.addEventListener(egret.TimerEvent.TIMER, this.roomTimeLoop, this);
		this.roomTimer.start();


		//	this.moveLoop.addEventListener(egret.TimerEvent.TIMER, this.updata, this);();
	}
	/**改变大小可见范围 */
	public changeVis(scal) {
		/**设置屏幕缩放比例 */
		//egret.Tween.get(this).to({ scaleX: scal, scaleY: scal }, 500);
		this.scaleX = this.scaleY = scal;
	}
	private roomTimeLoop() {
		if (this.superLayer.isUserDied)
			return;
		this.displayCountDownInfo(this.countDownTime--)
		if (this.countDownTime < 0) {
			if (!webNetworkMananger.isServerMove) {
				this.superLayer.playerDiedDis();
			}
			this.roomTimer.removeEventListener(egret.TimerEvent.TIMER, this.roomTimeLoop, this);
			this.roomTimer.reset();
		}
	}
	public frogKillMan(num: number) {
		this.killMan.visible = true;
		this.killMan.alpha = 1;
		this.killMan.scaleX = this.killMan.scaleY = 1;
		egret.Tween.get(this.killMan).to({ scaleX: 1.5, scaleY: 1.5 }, 1000)
		egret.Tween.get(this.killMan).to({ alpha: 0 }, 1000).call(() => { this.killMan.visible = false });
		this.killNum.visible = true;
		this.killNum.text = num + "";
		this.killNum.stroke = 1;
		this.killNum.strokeColor = 0xffffff;
		this.killNum.alpha = 1;
		this.killNum.scaleX = this.killNum.scaleY = 1;
		egret.Tween.get(this.killNum).to({ scaleX: 1.5, scaleY: 1.5 }, 1000)
		egret.Tween.get(this.killNum).to({ alpha: 0 }, 1000).call(() => { this.killNum.visible = false });
	}
	private backTohallInit() {
		this.backToHall.touchEnabled = true;
		this.backToHall.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
			if (webNetworkMananger.isServerMove) {
				let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
				let m_c2sBackToHall = new awesomepackage.c2sBackToHall();
				m_c2sBackToHall.account = DataProl.account;

				m_c2sBackToHall.sign = DataProl.sign;

				m_AwesomeMessage.meesageId = 1005;
				m_AwesomeMessage.data = awesomepackage.c2sBackToHall.encode(m_c2sBackToHall).finish();

				var buffer = awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
				var byte2: egret.ByteArray = new egret.ByteArray(buffer);
				webNetworkMananger.getInstance().SendBitMeesage(byte2);
				//		webNetworkMananger.getInstance().SendMeesage('backToHall', DataProl.account);
			}
			webNetworkMananger.getInstance().gameSceneRec.backToHall();
			DataProl.dei = 0;//一局内死亡次数清零
			this.countDownTime = -1;
			this.roomTimer.removeEventListener(egret.TimerEvent.TIMER, this.roomTimeLoop, this);
			this.roomTimer.reset();
			SoundManager.getInstance().playEffectSound("click_mp3");
		}, this);
	}
	/**返回大厅 */
	private back() {
		SoundManager.getInstance().playEffectSound("click_mp3");
		this.bgSound.closeSound();
		this.superLayer.backToHall();
		// if (webNetworkMananger.isServerMove)
		// 	webNetworkMananger.getInstance().SendMeesage('backToHall', DataProl.account);
		this.visible = false;
	}


	/**显示房间计时器 */
	public displayCountDownInfo(time: any) {
		var shi = Math.floor(time / 3600) < 10 ? "0" + Math.floor(time / 3600) : Math.floor(time / 3600);
		var fen = Math.floor(time / 60) < 10 ? "0" + Math.floor(time / 60) : Math.floor(time / 60);
		var miao = Math.floor(time % 60) < 10 ? "0" + Math.floor(time % 60) : Math.floor(time % 60);
		this.gameCountDownTime.text = shi + ":" + fen + ":" + miao;
	}
	/**显示玩家舌头长度 */
	public displayUserTongueLength(length: any) {
		this.userTongueLength.text = length;


	}
	/**显示玩家衣服颜色 */
	public displayUserClothesUpdata(shu: any) {
		//this.clothes.texture = RES.getRes(name);
		//this.changeClothes.yifu(this.clothes, shu)
	}

	/**从所有玩家身上获取排行 */
	public disposAllUser(data) {
		this.getUserTopMsg(data);
	}
	/**获取单人排行榜信息 */
	public getUserTopMsg(data) {
		let userlist = data;
		let player_data = [];
		let me_data = null;
		if (userlist.length > 0) {
			userlist.sort(function (a, b) { return b.grade - a.grade; });
			for (let i = 0; i < userlist.length; i++) {
				player_data.push({
					userId: userlist[i].userId,
					name: userlist[i].name,
					grade: userlist[i].grade,
					speed: userlist[i].speed
				});
			}
		} else {
			return;
		}
		this.showUserTopMsg(player_data);
	}
	/**显示单人排行榜信息 */
	public showUserTopMsg(playerData) {
		if (playerData.length == 0) {
			return;
		}
		this.resetUserTop();
		for (let i = 0; i < Math.min(6, playerData.length); i++) {
			let otherName = playerData[i].name.length > 7 ? playerData[i].name.substring(0, 7) : playerData[i].name;
			if (i == 0) {
				this.otherUserName.text = otherName;
				this.otherUserGrade.text = playerData[i].grade;
			} else {
				this.otherUserName.text += "\n" + otherName;
				this.otherUserGrade.text += "\n" + playerData[i].grade;
			}
		}
		for (let a = 0; a < playerData.length; a++) {
			if (playerData[a].userId == DataProl.account) {
				//显示自己的名字/分数/名次
				let myName = playerData[a].name.length > 7 ? playerData[a].name.substring(0, 7) : playerData[a].name;
				let rank = a + 1;
				this.myMsg.text = rank.toString() + "  " + myName;
				this.myMsg.textColor = 0x00FF00;
				this.myGrade.text = playerData[a].grade;
				this.myGrade.textColor = 0x00FF00;
			}
		}
	}
	// /**联网模式显示排行榜 */
	// public showNetTop(playerData, myData) {
	// 	if (playerData.length == 0 || myData == null) {
	// 		return;
	// 	}
	// 	this.resetUserTop();
	// 	for (let i = 0; i < Math.min(6, playerData.length); i++) {
	// 		//名字显示
	// 		let name_show = playerData[i].name.length > 8 ? playerData[i].name.substring(0, 8) : playerData[i].name;
	// 		this.playerName[i].text = name_show;
	// 		this.playerName[i].textColor = playerData[i].name == myData.name ? 0x00FF00 : 0xFFFFFF;
	// 		//分数显示
	// 		this.playerGrage[i].text = playerData[i].tongueLength;
	// 		this.playerGrage[i].textColor = playerData[i].name == myData.name ? 0x00FF00 : 0xFFFFFF;
	// 	}
	// 	//显示自己的名字/分数/名次
	// 	let myName = myData.name.length > 8 ? myData.name.substring(0, 8) : myData.name;
	// 	this.myName.text = myName;
	// 	this.myName.textColor = 0x00FF00;

	// 	this.myGrade.text = myData.tongueLength.toString();
	// 	this.myGrade.textColor = 0x00FF00;
	// 	//名次显示
	// 	for (let a = 0; a < playerData.length; a++) {
	// 		if (playerData[a].name == myData.name) {
	// 			let rank = a + 1;
	// 			this.myRanking.text = rank.toString();
	// 			return;
	// 		}
	// 	}


	// }

}